﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DataListSetting.aspx.cs" Inherits="ZX.WebApp.Extend.DataListSetting" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <!--CSS file (default YUI Sam Skin) -->
    <link rel="stylesheet" type="text/css" href="../scripts/yui_2.9.0/build/reset-fonts-grids/reset-fonts-grids.css" />
    <link rel="stylesheet" type="text/css" href="../scripts/yui_2.9.0/build/container/assets/skins/sam/container.css" />
    <link rel="stylesheet" type="text/css" href="../scripts/yui_2.9.0/build/menu/assets/skins/sam/menu.css" />
    <link rel="stylesheet" type="text/css" href="../scripts/yui_2.9.0/build/button/assets/skins/sam/button.css" />
    <link rel="stylesheet" type="text/css" href="../scripts/yui_2.9.0/build/resize/assets/skins/sam/resize.css" />
    <link rel="stylesheet" type="text/css" href="../scripts/yui_2.9.0/build/layout/assets/skins/sam/layout.css" />
    <link type="text/css" rel="stylesheet" href="../Scripts/yui_2.9.0/build/datatable/assets/skins/sam/datatable.css" />
    <!--begin custom header content for this example-->
    <style type="text/css">
    .yui-skin-sam table {
	    width: 100%;
    }
    
    #button-example-form fieldset, 
    #button-example-form fieldset div {

        border: 2px groove #ccc;
        margin: .5em;
        padding: .5em;

    }

    .yui-button#pushbutton2 button,
    .yui-button#pushbutton5 button, 
    .yui-button .Add button {
        background: url(../button/assets/add.gif) center center no-repeat;
        text-indent: -4em;
        overflow: hidden;
        padding: 0 .75em;
        width: 2em;
        *margin-left: 4em;   /* IE only */
        *padding: 0 1.75em;  /* IE only */

    }
    
    .yui-button#pushbutton3 button,
    .yui-button#pushbutton6 button, 
    .yui-button#pushbutton9 button {

        padding-left: 2em;
        background: url(../button/assets/add.gif) 10% 50% no-repeat;
    
    }
    #container select,#container input
    {
        width:100%;
        min-width:5em;
    }
    
    
    #toolbar
    {
        border-bottom:1px solid #ccc;
        margin-bottom:3px;
    }
    .form
    {
        padding:5px;
    }
    
    .form select
    {
        width:120px;
    }
    
    
</style>
    <!--end custom header content for this example-->
    <!-- Dependencies -->
    <script type="text/javascript" src="../Scripts/yui_2.9.0/build/yahoo-dom-event/yahoo-dom-event.js"></script>
    <script type="text/javascript" src="../Scripts/yui_2.9.0/build/element/element-min.js"></script>
    <script type="text/javascript" src="../Scripts/yui_2.9.0/build/datasource/datasource-min.js"></script>
    <!-- OPTIONAL: JSON Utility (for DataSource) -->
    <script type="text/javascript" src="../Scripts/yui_2.9.0/build/json/json-min.js"></script>
    <!-- OPTIONAL: Connection Manager (enables XHR for DataSource) -->
    <script type="text/javascript" src="../Scripts/yui_2.9.0/build/connection/connection-min.js"></script>
    <!-- OPTIONAL: Get Utility (enables dynamic script nodes for DataSource) -->
    <script type="text/javascript" src="../Scripts/yui_2.9.0/build/get/get-min.js"></script>
    <!-- OPTIONAL: Drag Drop (enables resizeable or reorderable columns) -->
    <script type="text/javascript" src="../Scripts/yui_2.9.0/build/dragdrop/dragdrop-min.js"></script>
    <!-- OPTIONAL: Calendar (enables calendar editors) -->
    <script type="text/javascript" src="../Scripts/yui_2.9.0/build/calendar/calendar-min.js"></script>
    <!-- Source files -->
    <script type="text/javascript" src="../Scripts/yui_2.9.0/build/datatable/datatable-min.js"></script>
    <script type="text/javascript" src="../scripts/yui_2.9.0/build/layout/layout-min.js"></script>
    <script type="text/javascript" src="../scripts/yui_2.9.0/build/button/button-min.js"></script>
    <script type="text/javascript" src="../scripts/yui_2.9.0/build/container/container-min.js"></script>
    <script type="text/javascript" src="../scripts/yui_2.9.0/build/resize/resize-min.js"></script>
   <%-- <script src="../Scripts/Appjs/model.TrSubjectInfo.js" type="text/javascript"></script>--%>
</head>
<body class="yui-skin-sam">
    <div id="toolbar" style="padding: 5px;">
<%--        <button type="button" id="btnOpen" class="yui-button yui-push-button" style="display: none">
            打开方案</button>--%>
        <button type="button" id="btnSave" class="yui-button yui-push-button">
        保存</button>
        <button type="button" id="btnAddNew" class="yui-button yui-push-button">
        新增</button>
    </div>
    <div class="form" style="display:none">
        历史方案:<% =Solution %>
        方案名称:<input type="text" id="SolutionName" /> 是否默认:<input type="checkbox" id="IsDefault" /></div>
    <div id="container">
    </div>
        <form id="form2" action="AjaxData/DataSave.aspx">
    <textarea style="display: none" id="jsonDataContent" rows="2" cols="2" name="jsonDataContent"></textarea>
    </form>
    <script type="text/javascript">
    // 字段信息
    var arrFieldInfo = <% =FieldInfo %>,
        arrOrAnd = [ { Text: " ", Value: " " },{ Text: "是", Value: "Y" }, { Text: "否", Value: "N" }],
        mDataTable,
        QueryInstance=[<% =strJson %>],
        const_Table = "<% =Table  %>"
        const_EntityName="<% =EntityName %>",
        const_FunctionId="<% =FunctionID %>",
        const_BlankInstance = [<%=Instance%>],
        const_SolutionList = <% =FieldList  %>;

        //格式化字段信息
        var formatField = function (elCell, oRecord, oColumn, sData) {
                elCell.innerHTML = "<input type=\"text\" value=\"" + sData + "\"  name='FieldName' onchange='ChangeEvent(this);'>";
            };
            var formatFiledType = function (elCell, oRecord, oColumn, sData) {
            }
            //格式化是否为空信息
            var formatIs_Null =  function(elCell, oRecord, oColumn, sData) {
                var str1 = [];
                var id = "id"+Math.random();
                str1.push("<select id='"+id+"' onchange='ChangeEvent(this);' name='Is_Null'>");
                for (i = 0; i < arrOrAnd.length; i++) {
                    if(arrOrAnd[i].Value == sData){
                    str1.push("<option value='" + arrOrAnd[i].Value + "' selected>" + arrOrAnd[i].Text + "</option>");
                    }else{
                    str1.push("<option value='" + arrOrAnd[i].Value + "'>" + arrOrAnd[i].Text + "</option>");
                    }
                    
                }
                str1.push("</select>");
                elCell.innerHTML = str1.join("");
            } 
            //格式化是否为主键信息
            var formatIs_PrimaryKey =  function(elCell, oRecord, oColumn, sData) {
                var str1 = [];
                var id = "id"+Math.random();
                str1.push("<select id='"+id+"' onchange='ChangeEvent(this);' name='Is_PrimaryKey'>");
                for (i = 0; i < arrOrAnd.length; i++) {
                    if(arrOrAnd[i].Value == sData){
                    str1.push("<option value='" + arrOrAnd[i].Value + "' selected>" + arrOrAnd[i].Text + "</option>");
                    }else{
                    str1.push("<option value='" + arrOrAnd[i].Value + "'>" + arrOrAnd[i].Text + "</option>");
                    }
                    
                }
                str1.push("</select>");
                elCell.innerHTML = str1.join("");
            } 
            //格式化左括符信息
            var formatLeftText = function (elCell, oRecord, oColumn, sData) {
                elCell.innerHTML = "<input type=\"text\" value=\"" + sData + "\"  name='Left' onchange='ChangeEvent(this);'>";
            }
            //格式化右括符信息
            var formatRightText = function (elCell, oRecord, oColumn, sData) {
                elCell.innerHTML = "<input type=\"text\" value=\"" + sData + "\"  name='Right' onchange='ChangeEvent(this);'>";
            }
            //格式化长度/精确度信息
           var formatLengthText = function (elCell, oRecord, oColumn, sData) {
                elCell.innerHTML = "<input type=\"text\" value=\"" + sData + "\"  name='Length' onchange='ChangeEvent(this);'>";
            }
            //格式化说明信息
            var formatNotesText = function (elCell, oRecord, oColumn, sData) {
                elCell.innerHTML = "<input type=\"text\" value=\"" + sData + "\"  name='Notes' onchange='ChangeEvent(this);'>";
            }
   

    var myDataSource;
    //YAHOO创建表
    YAHOO.util.Event.addListener(window, "load", function () {
          YAHOO.example.RowDataMod = function () {
            
            //定义表头
            var myColumnDefs = [
            
            //{ key: "FieldName", label: "左括符", resizeable: true },
            { key: "EntityName", label: "实体名", resizeable: true, formatter: formatField },
            { key: "Field", label: "字段名", resizeable: true, formatter: formatFiledType },
            { key: "THCaption", label: "列名", resizeable: true, formatter: formatLengthText },
            { key: "Width", label: "宽度", resizeable: true, formatter: formatIs_Null },
            { key: "IsVisiable", label: "是否显示", resizeable: true, formatter: formatIs_PrimaryKey },            
            { key: "IsQuery", label: "是否快搜", resizeable: true, formatter: formatRightText },
            { key:"iOrder",label:"排序",resizeable: true,formatter: formatNotesText},
            { key: "DataFormatter", label: "格式化方法", resizeable: true, formatter: formatLeftText },
          ];
           
            //定义数据源字段名
            myDataSource = new YAHOO.util.DataSource([]);
            myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;
            myDataSource.responseSchema = {
                fields: ["TableFieldID","FieldName","TableID", "TableName", "Is_PrimaryKey", "FiledType", "Length","Is_CustomDefine","Notes","Is_Null"]
            };
            
            //创建表
            var myDataTable = new YAHOO.widget.DataTable("container",
                myColumnDefs, myDataSource, {selectionMode:"single"});
            mDataTable = myDataTable;
            var i = 1,
            bReverseSorted = false;

            //排序
            // Track when Column is reverse-sorted, since new data will come in out of order
            var trackReverseSorts = function (oArg) {
                bReverseSorted = (oArg.dir === YAHOO.widget.DataTable.CLASS_DESC);
            };

            //注册事件
            myDataTable.subscribe("rowMouseoverEvent", myDataTable.onEventHighlightRow); 
	        myDataTable.subscribe("rowMouseoutEvent", myDataTable.onEventUnhighlightRow); 
            myDataTable.subscribe("rowClickEvent", myDataTable.onEventSelectRow); 
            //this.myDataTable.subscribe("rowDblclickEvent", onRowDoubleClick);
            myDataTable.subscribe("checkboxClickEvent", function(oArgs){
                var elCheckbox = oArgs.target;
                var oRecord = this.getRecord(elCheckbox);
                oRecord.setData("iSun_Selected",elCheckbox.checked);
            });

            // Add/update/delete rows as indicated
            var handleClick = function () {
                // Reset sort
                myDataTable.set("sortedBy", null);

                index = myDataTable.getRecordSet()._records.length;
                var entity = const_BlankInstance;
                myDataTable.addRows(entity, index);

                YAHOO.log("Could not continue due to invalid index.");
            }

             
            var fnDelete = function(){
                 var recordID = myDataTable.getSelectedRows()[0];
                 myDataTable.deleteRow(recordID);
                 //alert(recordID);
            }

            //保存
            var Save = function ()
            {
                    //setQuery();
                    //debugger
                    var list =myDataTable.getRecordSet()._records;
                    var elist = [];
                    for(var i=0;i<list.length;i++)
                    {
                      if(list[i]._oData["TableFieldID"]==""||list[i]._oData["TableFieldID"]=="00000000-0000-0000-0000-000000000000"){
                          elist.push(list[i]._oData);
                          }
                    }
                   
                    url = "AjaxData/DataSave.aspx";
                    var jsonStr = YAHOO.lang.JSON.stringify(elist);
                    //var condition = getCondition();

                    //QueryInstance[0].EntityOjbectList = jsonStr;
                    //生成数据
                    //jsonStr =  YAHOO.lang.JSON.stringify(QueryInstance[0]);
                    //alert(jsonStr);
                    var TextCtrl = document.getElementById("jsonDataContent");
                    TextCtrl.value = jsonStr;
                     var formObject = document.getElementById('form2');
                     YAHOO.util.Connect.setForm(formObject);
                    var SaveUrl =url+"?r=" + Math.random() + "&flag=1&action=setTableStruc&functionid="+const_FunctionId+"&entity="+const_EntityName;//+"&json=" + encodeURI(jsonStr.replace(/&/g,"[A]").replace(/'/g,"''"));
                    YAHOO.util.Connect.asyncRequest('POST', SaveUrl, {
                        success: function (res) {
                            try {
                                var str = res.responseText;
                                if(str=="1"){
                                    alert("数据保存成功");
                                    parent.closeWin();
                                }
                                else
                                {
                                    alert(str+"\n"+"数据保存失败!");
                                }
                            }
                            catch (e) {
                            }
                            finally {
                            }
                        },
                        failure: function (error) {
                            alert("数据错误：" + error.statusText);
                        }
                    }, null);
                }


            var btnAdd = new YAHOO.widget.Button("btnAddNew");
            btnAdd.on("click", handleClick);
            var btnSave = new YAHOO.widget.Button("btnSave");
            btnSave.on("click",Save);
            debugger
            SetFormValue();
            return {
                ds: myDataSource,
                dt: myDataTable
            };

           
        } ();
    });


    //值改变事件
    var ChangeEvent = function(oArgs){
        var f = oArgs.name;
        //alert(f);
        var v = oArgs.value;
        //alert(v);
        setValue(f,v);
    }

    ///f=字段名 v=值
    var setValue = function(f,v){
        var recordID = mDataTable.getSelectedRows()[0];
        var record = mDataTable.getRecord(recordID);
        record._oData[f]=v;    
    }


    var ChangeSolution = function(obj){
        //alert(obj.value);

        for(var i=0;i<const_SolutionList.length;i++){
            if(const_SolutionList[i].QueryId==obj.value)
            {
                SetFormValue(const_SolutionList[i]);
            }
        }

        //const_SolutionList 
    }
    
    var SetFormValue = function(){
        //debugger
        var arr = const_SolutionList;
        myDataSource.liveData = arr;
        mDataTable.getDataSource().sendRequest(null,
        {success: mDataTable.onDataReturnInitializeTable},
        mDataTable);

       // mDataTable.render();
       // document.getElementById("SolutionName").value = obj.SolutionName;
       // document.getElementById("IsDefault").checked = QueryInstance[0].IsDefault == "Y" ? true : false;
    }

    
    </script>
    <!--END SOURCE CODE FOR EXAMPLE =============================== -->
</body>
</html>
